package com.woniuxy.handler;

import com.woniuxy.feign.UserFeign;
import com.woniuxy.service.ChatService;
import com.work.framework.pojo.Chat;
import com.work.framework.pojo.ResultDTO;
import com.work.framework.pojo.User;
import com.work.framework.pojo.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.List;

/**
 * @Author: wanghao
 * @Date: 2019/9/17 20:19
 */
@Api(value = "处理聊天方面的请求",tags = "卖家，买家通讯录")
@RestController
@RequestMapping("/chat")
public class ChatHandler {
    @Resource
    private ChatService chatService;


    @ApiOperation(value = "卖家和买家互相添加通讯录")
    @PostMapping("/addChat/{p_uid}/{b_uid}")
    public ResultDTO addChat(@PathVariable Integer p_uid,@PathVariable Integer b_uid) {
        //数据验证
        if (p_uid == null || b_uid == null || p_uid.equals(b_uid)) {
            return null;
        }
        Chat chat = new Chat();
        UserInfo b_userInfo = new UserInfo();
        b_userInfo.setUid(b_uid);
        chat.setB_uInfo(b_userInfo);
        UserInfo p_userInfo = new UserInfo();
        p_userInfo.setUid(p_uid);
        chat.setB_uInfo(p_userInfo);
        //调用service加入数据库
        Integer insert = chatService.addChat(chat);
        ResultDTO resultDTO = new ResultDTO();
        if (insert == null || insert == 0) {
            resultDTO.setCode(500);
            resultDTO.setMessage("请联系管理员");
            return resultDTO;
        } else if (insert < 0) {
            resultDTO.setCode(500);
            resultDTO.setMessage("已经建立通讯录");
            return resultDTO;
        }
        resultDTO.setCode(200);
        resultDTO.setMessage("成功添加");
        return resultDTO;
    }

    @ApiOperation(value = "买家和卖家删除通讯录")
    @DeleteMapping("/deleteChat/{cid}")
    public ResultDTO deleteChat(@PathVariable Integer cid) {
        //数据验证
        if (cid == null) {
            return null;
        }
        //删除通讯录
        Integer delete = chatService.deleteChat(cid);
        ResultDTO resultDTO = new ResultDTO();
        if (delete <= 0) {
            resultDTO.setCode(500);
            resultDTO.setMessage("通讯录删除失败");
            return resultDTO;
        }
        resultDTO.setCode(200);
        resultDTO.setMessage("通讯录删除成功");
        //数据处理
        return resultDTO;
    }

    @ApiOperation(value = "查找用户的卖家通讯录")
    @GetMapping("/findSeller/{p_uid}")
    public ResultDTO findSeller(@PathVariable Integer p_uid) {
        //数据验证
        if (p_uid == null) {
            return null;
        }
        //调用service查找卖家通讯录
        List<Chat> chats = chatService.findSeller(p_uid);
        ResultDTO resultDTO = new ResultDTO();
        if (chats == null) {
            resultDTO.setCode(500);
            resultDTO.setMessage("服务器错误");
            return resultDTO;
        }
        resultDTO.setCode(200);
        resultDTO.setData(chats);
        resultDTO.setMessage("查询卖家通讯录成功");
        return resultDTO;
    }

    @ApiOperation(value = "查找用户的买家通讯录")
    @GetMapping("/findBuyer")
    public ResultDTO findBuyer(Integer b_uid) {
        //数据验证
        if (b_uid == null) {
            return null;
        }
        //调用service查找买家通讯录
        List<Chat> chats = chatService.findBuyer(b_uid);
        ResultDTO resultDTO = new ResultDTO();
        if (chats == null) {
            resultDTO.setCode(500);
            resultDTO.setMessage("服务器错误");
            return resultDTO;
        }
        resultDTO.setCode(200);
        resultDTO.setData(chats);
        resultDTO.setMessage("查询买家通讯录成功");
        return resultDTO;
    }

    @ApiOperation(value = "设置")
    @PostMapping("/setKey")
    public Object setKey(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("name", "小时候很驴");
        System.out.println("这是一被设置了");
        return request.getSession().getId();
    }

    @ApiOperation(value = "获取")
    @GetMapping("/getKey")
    public Object getKey(HttpServletRequest request) {
        HttpSession session = request.getSession();
        System.out.println(session.getId());
        System.out.println(session.getAttribute("user"));
        return session.getAttribute("name");
    }

}
